package net.wenku8.reader.view.common

import android.net.Uri
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.wenku8.reader.enums.ColorType
import net.wenku8.reader.enums.KeySettingBg
import net.wenku8.reader.enums.KeySettingFont
import net.wenku8.reader.enums.KeySettingSpace
import net.wenku8.reader.service.FileService
import net.wenku8.reader.ui.theme.LiteGray

class ReadSettingModel : ViewModel() {
    //设置相关
    var fontSize by mutableStateOf(17.sp)
    var bgColor by mutableStateOf(LiteGray)
    var textColor by mutableStateOf(Color.Black)
    var spaceSize by mutableStateOf(10.dp)

    var fontValue by mutableIntStateOf(17)
    var bgValue by mutableIntStateOf(0)
    var spaceValue by mutableIntStateOf(10)

    var show by mutableStateOf(false)

    private val fontMax = 28
    private val fontMin = 10

    private val spaceMax = 20
    private val spaceMin = 0

    fun initData(){
        fontValue = MMKV.defaultMMKV().decodeInt(KeySettingFont, 17)
        bgValue = MMKV.defaultMMKV().decodeInt(KeySettingBg, 0)
        spaceValue = MMKV.defaultMMKV().decodeInt(KeySettingSpace, 10)

        fontSize = TextUnit(fontValue.toFloat(), TextUnitType.Sp)
        spaceSize = Dp(spaceValue.toFloat())
        val colorType = ColorType.entries.first { colorType -> colorType.type == bgValue }
        bgColor = colorType.bgColor
        textColor = colorType.textColor
    }

    fun changeFontSize(inc : Boolean){
        if (inc && fontValue >= fontMax || !inc && fontValue <= fontMin){
            return
        }
        if (inc) fontValue++ else fontValue--
        fontSize = TextUnit(fontValue.toFloat(), TextUnitType.Sp)
        MMKV.defaultMMKV().encode(KeySettingFont, fontValue)
    }

    fun changeBgColor(bgColorType : ColorType){
        bgValue = bgColorType.type
        bgColor = bgColorType.bgColor
        textColor = bgColorType.textColor
        MMKV.defaultMMKV().encode(KeySettingBg, bgValue)
    }

    fun changeSpaceSize(inc : Boolean){
        if (inc && spaceValue >= spaceMax || !inc && spaceValue <= spaceMin){
            return
        }
        if (inc) spaceValue++ else spaceValue--
        spaceSize = Dp(spaceValue.toFloat())
        MMKV.defaultMMKV().encode(KeySettingSpace, spaceValue)
    }

    fun selectFile(uri: Uri?){
        if (uri == null) return
        viewModelScope.launch(Dispatchers.IO) {
            FileService.copyFile(uri, "/setting/readBgImage")
            MMKV.defaultMMKV().encode(KeySettingBg, ColorType.TYPE99.type)
            changeBgColor(ColorType.TYPE99)
        }
    }

}
